package com.cicadamob.contact.api.service;
import com.cicadamob.contact.api.Utils.Data.File.FileManager;
import com.cicadamob.contact.api.Utils.Data.File.FileUtil;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;

/**
 * 写log IntentService
 * 
 * @author mengxs
 * 
 */
public class WriteLogIntentService extends IntentService {
	/** log路径 **/
	private static final String FOLDER_DIR = "folderDir";
	/** log名称 **/
	private static final String FILE_NAME = "fileName";
	/** log内容 **/
	private static final String LOG_MESSAGE = "logMessage";
	/** 是否每日一log **/
	private static final String LOG_PERDAY = "logPerDay";
	
	// TODO: Rename actions, choose action names that describe tasks that this
    // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
    private static final String ACTION_WRITE_LOG_BAZ = "com.gmccgz.kakaonew.sipcall.service.action.WARITE_LOG_BAZ";
private static final String ACTION_WIRTE_LOG_FOO="com.gmccgz.kakaonew.sipcall.service.action.WARITE_LOG_FOO";
    // TODO: Rename parameters
    private static final String EXTRA_PARAM1 = "com.gmccgz.kakaonew.sipcall.service.extra.PARAM1";
    private static final String EXTRA_PARAM2 = "com.gmccgz.kakaonew.sipcall.service.extra.PARAM2";


	/**
	 * 写log到文件
	 * 
	 * @param context
	 * @param folderDir
	 *            log目录
	 * @param fileName
	 *            log文件名称
	 * @param logMessage
	 *            log内容
	 * @param logPerDay
	 *            是否每日一个log文件
	 */

	public static void writeFileLog(Context context, String folderDir,
	    String fileName, String logMessage, boolean logPerDay) {
		Intent intent = new Intent(context, WriteLogIntentService.class);
		intent.putExtra(FOLDER_DIR, folderDir);
		intent.putExtra(FILE_NAME, fileName);
		intent.putExtra(LOG_MESSAGE, logMessage);
		intent.putExtra(LOG_PERDAY, logPerDay);
		intent.setAction(ACTION_WIRTE_LOG_FOO);
		context.startService(intent);				
	}

	 /**
     * Starts this service to perform action Baz with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */
    // TODO: Customize helper method
    public static void startActionBaz(Context context, String param1, String param2) {
        Intent intent = new Intent(context, WriteLogIntentService.class);
        intent.setAction(ACTION_WRITE_LOG_BAZ);
        intent.putExtra(EXTRA_PARAM1, param1);
        intent.putExtra(EXTRA_PARAM2, param2);
        context.startService(intent);
    }
	public WriteLogIntentService() {
		super("com.gmccgz.kakaonew.Utils.Logger.WriteLogIntentService");
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		if (intent != null) {
			 final String action = intent.getAction();
	            if (ACTION_WRITE_LOG_BAZ.equals(action)) {
	                final String param1 = intent.getStringExtra(EXTRA_PARAM1);
	                final String param2 = intent.getStringExtra(EXTRA_PARAM2);
	                handleActionFoo(param1, param2);
	            } else if (ACTION_WIRTE_LOG_FOO.equals(action)) {
	              
	            	String folderDir = intent.getStringExtra(FOLDER_DIR);
	            	String fileName = intent.getStringExtra(FILE_NAME);
	            	String logMessage = intent.getStringExtra(LOG_MESSAGE);
	            	boolean logPerDay = intent.getBooleanExtra(LOG_PERDAY, true);
	            	FileUtil.writeLogToFile(folderDir, fileName, logMessage, logPerDay);
	            }
		}
	}
	 /**
     * Handle action Foo in the provided background thread with the provided
     * parameters.
     */
    private void handleActionFoo(String param1, String param2) {
        // TODO: Handle action Foo
        FileManager.createLogtoFile(param1,param2);
    }
}
